//
//  ViewController.m
//  网易上面的滑动栏
//
//  Created by mafeng on 16/8/3.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#import "LabelWaterLayout.h"
#import "MyCollectionViewCell.h"
#import "SelfSizeTool.h"
#define kCollectionViewCell @"reuse"
#define kRandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1]
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, LabelWaterLayoutDelegate>
@property (nonatomic, strong) NSArray *array ;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // scrollView自动边距
    // 遇到状态栏和导航栏, 自动为页面上的某一个scrollView添加边距
    // 一般开发中关闭这个属性
    // 如果不关闭这个属性, 会不明加一些边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
//    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//    flowLayout.itemSize = CGSizeMake(100, 30);
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    flowLayout.minimumLineSpacing = 3;
    self.array = @[@"新闻", @"体育", @"直播", @"哒哒趣闻", @"中国足球", @"CBA", @"公开课", @"影视剧"];
    
    
    
    LabelWaterLayout *layout = [[LabelWaterLayout alloc] init];
    layout.delegate = self;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 30) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor yellowColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    // 注册要改为nib
    [collectionView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kCollectionViewCell];
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCell forIndexPath:indexPath];
    cell.label.text = _array[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = kRandomColor;
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout height:(CGFloat)height widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGRect rect = [_array[indexPath.row] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
    // ceilf() 这个方法可以向上取整,进一法
    CGFloat width = ceilf(rect.size.width);
    return width;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
